<?php
/**
 * Created by PhpStorm.
 * User: Zhangxy
 * Date: 2018/8/17
 * Time: 20:36
 */

namespace App\Traits;

trait LeTouApiResponse
{
    protected $returnData = [];
    protected $code = 1;

    protected $message = "";
    protected $data = "";

    /**
     * @param int $code
     */
    public function setCode(int $code)
    {
        $this->code = $code;
    }

    /**
     * @param string $message
     */
    public function setMessage(string $message)
    {
        $this->message = $message;
    }

    /**
     * @param string $data
     */
    public function setData(string $data)
    {
        $this->data = $data;
    }


    /**
     * 返回成功
     * @param array $data
     * @param string $message
     * User: Zhangxy
     * Date: 2018-01-0
     * @return $this
     */
    public function success($data = [], $message = "操作成功")
    {
        $this->returnData = [
            'code' => 0,
            'message' => $message,
            'data' => $data,
        ];

        return response()->json($this->returnData)->setCallback(request()->input('callback'));
    }


    /**
     * 返回失败
     * @param int $code
     * @param string $message
     * User: Zhangxy
     * Date: 2018-01-0
     * @return $this
     */
    public function failed($code = 1, $message = "操作失败")
    {
        $this->returnData = [
            'code' => $code,
            'message' => $message,
            'data' => $this->data,
        ];
        return response()->json($this->returnData)->setCallback(request()->input('callback'));
    }

    /**
     * 返回 自定义
     * @param int $code
     * @param string $message
     * @param array $data
     * User: Zhangxy
     * Date: 2018-01-0
     * @return $this
     */
    public function respond($code = 1, $message = "操作失败", $data = [])
    {
        if (!empty($code)) {
            $this->setCode($code);
        }
        if (!empty($message)) {
            $this->setMessage($message);
        }
        if (!empty($data)) {
            $this->setData($data);
        }

        $this->returnData = [
            'code' => $this->code,
            'message' => $this->message,
            'data' => $this->data,
        ];
        return response()->json($this->returnData)->setCallback(request()->input('callback'));
    }


}